@echo off
setlocal enabledelayedexpansion
color 0a

set CURR_DIR=%cd%
set distdir=%CURR_DIR%\package\更新包
set bakdir=%CURR_DIR%\package\系统备份
set appdir=%CURR_DIR%\webapps



set JAVA_HOME=%cd%\env\jdk1.8.0_91
set M2_HOME=%CURR_DIR%\env\apache-maven-3.3.9
set TOMCAT_HOME=%CURR_DIR%\env\apache-tomcat-8.0.52
set CATALINA_HOME=%TOMCAT_HOME%
set ymd=%date:~0,4%%date:~5,2%%date:~8,2%
set hms=%time:~0,2%%time:~3,2%%time:~6,2%


echo -------------------------------------------
echo   JAVA_HOME:%JAVA_HOME%
echo     M2_HOME:%M2_HOME%
echo TOMCAT_HOME:%TOMCAT_HOME%


echo -------------------------------------------
echo 请选择更新包类型：
echo 【1】从MAVEN构建构建完整war包
echo 【2】从以下位置选择完整war包：
echo 	  %distdir%\main.war
echo 【3】从以下位置使用增量更新包：
echo 	  %distdir%\main.zip
set /p buildMethod=请选择：
goto buildMethod-%buildMethod%



rem 从maven构建war包
:buildMethod-1
rem 执行mvn构建 start----------------
if exist ./src/MIS/iams (
	echo "代码已下载，执行SVN更新代码操作。"
	svn update ./src/MIS/
) else (
	set /p user=请输入SVN账号：
	set /p pwd=请输入SVN密码：

	svn checkout http:/xxxxxxxxx/ ./src/MIS --no-auth-cache --username !user! --password !pwd!
)
if %ERRORLEVEL% NEQ 0 (
	echo svn error!
	goto end
)
cd .\src\MIS\
set /p isRePackage=是否执行MAVEN构建？n（y/n） 
if !isRePackage!==y (
	start "maven打包" cmd /k %M2_HOME%\bin\mvn clean install
	
	echo -------------------------------------------
	echo "请等待打包完成..."
	@pause
	
	rem 拷贝war包
	if exist %CURR_DIR%\src\MIS\iams\target\main.war (
		echo -------------------------------------------
		echo copy main.war to %distdir%
		copy %CURR_DIR%\src\MIS\iams\target\main.war %distdir%
	)
) else (
	echo -------------------------------------------
	echo 取消MAVEN构建.
)
rem 执行mvn打包 end----------------


rem 指定war包更新方式
:buildMethod-2

if exist %distdir%\main.war (
	set warname=main.%ymd%%hms%.war
	cd %distdir%
	rename main.war !warname!
	
	echo -------------------------------------------
	echo 已将%distdir%\main.war 修改为!warname!
	
	if exist %appdir%\main (
		echo -------------------------------------------
		echo 将%appdir%\main 移动至 %bakdir%\main.%ymd%%hms%
		move %appdir%\main %bakdir%\main.%ymd%%hms%
	)
	echo -------------------------------------------
	echo 将%distdir%\!warname!解压到%appdir%\main
	%CURR_DIR%\env\7z1805-extra\7za.exe x %distdir%\!warname! -o%appdir%\main
	
	if %ERRORLEVEL% NEQ 0 (
		echo 解压出错!
		goto end
	)
	
	echo -------------------------------------------
	echo 解压完毕

) else (
	echo -------------------------------------------
	echo "WAR包:%CURR_DIR%\src\MIS\iams\target\main.war不存在."
)



rem 增量更新方式
:buildMethod-3
if exist %distdir%\main.zip (
	set zipname=main.patch.%ymd%%hms%.zip
	cd %distdir%
	rename main.zip !zipname!

	echo -------------------------------------------
	echo 将%distdir%\!zipname!解压到%appdir%\main
	%CURR_DIR%\env\7z1805-extra\7za.exe x %distdir%\!zipname! -o%appdir%\main
	
	if %ERRORLEVEL% NEQ 0 (
		echo 解压出错!
		goto end
	)
	
	echo -------------------------------------------
	echo 解压完毕
)
rem buildMethod-3 end



echo -------------------------------------------
set /p isUpdateConf=是否更新配置文件？n（y/n）：
if %isUpdateConf%==y (
	echo -------------------------------------------
	xcopy %CURR_DIR%\conf\main %appdir%\main /E /Y
)

echo -------------------------------------------
set /p isStartTomcat=是否启动Tomcat？n（y/n）
if /I !isStartTomcat!==y (
	start "停止Tomcat" cmd /c %TOMCAT_HOME%\bin\shutdown.bat
	%TOMCAT_HOME%\bin\startup.bat
)







:end
@pause


